package contegor.scire.core;

import java.util.Set;

import contegor.scire.core.ConnectableImpl.Status;

/**
 * Basic {@link Task} that checks if there are {@link Link}s in the target {@link Container}
 * who's target is the one given. If if there are, it will send back the {@link Id}
 * of such {@link Container}.
 */
public class AreLinksAimingTargetId extends TaskImpl<Id> {
  private static final long serialVersionUID = 8349839053533147454L;

  private Id targetId;

  public AreLinksAimingTargetId(TaskId taskId, Id targetContainerId, Id targetId) {
    super(taskId, targetContainerId);
    this.targetId = targetId;
  }

  @Override
  public void process(Container targetContainer) {
    Set<Link> links = targetContainer.getLinksReferring(targetId);
    if ((links != null) && (links.size() > 0))
      for (Link link: links) {
        if (this.taskId.getTransactionId().equals(((LinkImpl)link).transactionId.get())) {
          if (!Status.ERASED.equals(((LinkImpl)link).status.get())) {
            this.returnPoint.manage(this.taskId, this.targetContainerId);
            return;
          }
        }
        else {
          this.returnPoint.manage(this.taskId, this.targetContainerId);
          return;
        }
      }
  }
}